using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMM.Core.Domain;
using ServiceStack.ServiceHost;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MMM.Services.Base;
using MMM.Services.Operations;
using MMM.Services.Type;
using ServiceStack.ServiceInterface;
using MMM.Core;
using MMM.Services.Response;

namespace MMM.Services
{

    public class MetaDataService : BaseService<MetaData>
    {
    }

    public class MetaDataGetSerice : BaseGetService<MetaDataPageGet, MetaData>
    {
        //protected override System.Linq.Expressions.Expression<Func<MetaData, bool>> CreateFilter(MetaDataPageGet request)
        //{
        //    return base.CreateFilter(request);
        //}
        protected override IList<Order> GetSort(MetaDataPageGet request)
        {

            IList < Order > lst= base.GetSort(request);
            if (lst.Count==0)
            {
             lst.Add(new Order{ property="FValue",direction="asc"});   
            }
            return lst;
        }
    }
    public class CheckMetaDataService : RestServiceBase<CheckMetaData>
    {
        IRepository<MetaData> mdRep = ApplicationContext.Container.GetInstance<IRepository<MetaData>>();
        IRepository<Function> funcRep = ApplicationContext.Container.GetInstance<IRepository<Function>>();
        public override object OnGet(CheckMetaData request)
        {
            List<CheckMetaData> lst = new List<CheckMetaData>();
            var mdLst = mdRep.GetAll().OrderBy(s => s.FValue);
            int value = 0;
            if (request.funcID != null)
            {
                Function function = funcRep.GetEntity(request.funcID);
                value = function.Value;
            }

            foreach (var item in mdLst)
            {
                CheckMetaData check = new CheckMetaData();
                check.Id = item.Id;
                check.FOrder = item.FOrder;
                check.FText = item.FText;
                check.FValue = item.FValue;
                if (value != 0)
                {
                    check.IsChecked = IsCheckIng(value, check.FValue);
                }

                lst.Add(check);
            }
            return new OperationResult<CheckMetaData>(lst, lst.Count());
        }

        private bool IsCheckIng(int value, int fValue)
        {
            return (value & fValue) == fValue;
        }
    }

    public class CheckMetaDataListService : RestServiceBase<CheckMetaDataList>
    {
        IRepository<Function> funcRep = ApplicationContext.Container.GetInstance<IRepository<Function>>();
        public override object OnPut(CheckMetaDataList request)
        {
            if (request.Count() > 0)
            {
                Function function = funcRep.GetEntity(request.funcID);
                foreach (var item in request)
                {
                    if (item.IsChecked)
                    {
                        function.Value += item.FValue;
                    }
                    else
                    {
                        function.Value -= item.FValue;
                    }
                }
                funcRep.Update(function);
            }
            ClearCash();
            return OperationResult<CheckMetaDataList>.SuccessRespose;
        }
        public void ClearCash()
        {
            this.GetCacheClient().Remove("functions");
            this.GetCacheClient().Remove("dd");
            this.GetCacheClient().Remove("roleList");
            this.GetCacheClient().Remove("userPerList");
            this.GetCacheClient().Remove("funList");
        }
    }
    [Serializable]
    [Route("/api/checkmetadata", "PUT")]
    public class CheckMetaDataList : List<CheckMetaData>
    {
        public Guid? funcID { get; set; }
    }

    [Serializable]
    [Route("/api/checkmetadata", "GET")]
    public class CheckMetaData : MetaData
    {
        public CheckMetaData()
        {
            IsChecked = false;
        }
        public bool IsChecked { get; set; }
        public Guid? funcID { get; set; }
    }
}
